package com.example.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.example.config.ApplicationContextHelper;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import javax.sql.XADataSource;
import java.util.List;
import java.util.Map;

@Repository
public class BaseDao{

    protected JdbcTemplate jdbcTemplate1;

    protected JdbcTemplate jdbcTemplate2;

    protected JtaTransactionManager transactionManager;

    @Autowired
    private ApplicationContextHelper applicationContextHelper;

    public BaseDao(){

        //        DruidDataSource db1 = applicationContextHelper.popBean("db1", DruidDataSource.class);
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setXaDataSource(db1);
//        //        DruidDataSource db1 = (DruidDataSource)applicationContextHelper.getApplicationContext().getBean("db1");
//        this.jdbcTemplate1 = new JdbcTemplate(db1);
//        DruidDataSource db2 = applicationContextHelper.popBean("db2", DruidDataSource.class);
//        this.jdbcTemplate2 = new JdbcTemplate(db2);
//        EnhancedTomcatAtomikosBeanFactory
//        DruidDataSourceFactory factory = new DruidDataSourceFactory();

        this.jdbcTemplate1 = applicationContextHelper.popBean("jdbcTemplate1", JdbcTemplate.class);
        this.jdbcTemplate2 = applicationContextHelper.popBean("jdbcTemplate2", JdbcTemplate.class);



//        XADataSource db1 = applicationContextHelper.popBean("db1", XADataSource.class);
//        AtomikosDataSourceBean atomikosDataSourceBean1 = new AtomikosDataSourceBean();
//        atomikosDataSourceBean1.setXaDataSource(db1);
//        atomikosDataSourceBean1.setUniqueResourceName("db1");
//        this.jdbcTemplate1 = new JdbcTemplate(atomikosDataSourceBean1);
//        XADataSource db2 = applicationContextHelper.popBean("db2", XADataSource.class);
//        AtomikosDataSourceBean atomikosDataSourceBean2 = new AtomikosDataSourceBean();
//        atomikosDataSourceBean2.setXaDataSource(db2);
//        atomikosDataSourceBean1.setUniqueResourceName("db2");
//        this.jdbcTemplate2 = new JdbcTemplate(atomikosDataSourceBean2);


//        DruidDataSource db1 = applicationContextHelper.popBean("db1", DruidDataSource.class);
//        this.jdbcTemplate1 = new JdbcTemplate(db1);
//        DruidDataSource db2 = applicationContextHelper.popBean("db2", DruidDataSource.class);
//        this.jdbcTemplate2 = new JdbcTemplate(db2);
//
        UserTransactionManager userTransactionManager = applicationContextHelper.popBean("atomikosTransactionManager", UserTransactionManager.class);
        UserTransactionImp  userTransactionImp = applicationContextHelper.popBean("atomikosUserTransaction", UserTransactionImp.class);
        this.transactionManager = new JtaTransactionManager(userTransactionImp, userTransactionManager);
    }

//    public void setJndiObjectFactoryBean(JndiObjectFactoryBean jndiObjectFactoryBean) {
//        this.jndiObjectFactoryBean = jndiObjectFactoryBean;
//    }

//    protected JndiTemplate jndiTemplate;

//    public void setJndiObjectFactoryBean(JndiObjectFactoryBean factoryBean) {
//        DataSource dataSource1 = (DataSource)factoryBean.getObject();
//        jdbcTemplate = new JdbcTemplate(dataSource1);
//        List list = jdbcTemplate.queryForList("select * from test");
//        System.out.println(list.size());
//    }
//public BaseDao(){
////    JndiObjectFactoryBean db1 = applicationContextHelper.popBean("db1", JndiObjectFactoryBean.class);
////    System.out.println(db1);
//    //    DataSource dataSource1 = (DataSource)db1.getObject();
////    jdbcTemplate = new JdbcTemplate(dataSource1);
////    List list = jdbcTemplate.queryForList("select * from test");
////            System.out.println(list.size());
//}

//    public BaseDao(JndiObjectFactoryBean jndiObjectFactoryBean) {
//        this.jndiObjectFactoryBean = jndiObjectFactoryBean;
//        DataSource dataSource1 = (DataSource)jndiObjectFactoryBean.getObject();
//        jdbcTemplate = new JdbcTemplate(dataSource1);
//    }

//    public void setJndiObjectFactoryBean(JndiObjectFactoryBean jndiObjectFactoryBean) {
//    this.jndiObjectFactoryBean = jndiObjectFactoryBean;
//    }
//    public BaseDao(JndiObjectFactoryBean factoryBean) {
////        jndiTemplate = factoryBean.getJndiTemplate();
////        DataSource dataSource = (DataSource)factoryBean.getObject();
////        jdbcTemplate = new JdbcTemplate(dataSource);
////        List list = jdbcTemplate.queryForList("select * from test");
////            System.out.println(list.size());
////        Properties properties = jndiTemplate.getEnvironment();
//
//    }



//    public void setJndiObjectFactoryBean(JndiObjectFactoryBean jndiObjectFactoryBean) {
//    }


//    public void setDataSource(JndiObjectFactoryBean dataSource) {
////        this.dataSource = (DataSource)dataSource;
//        jndiTemplate = dataSource.getJndiTemplate();
//        Properties properties = jndiTemplate.getEnvironment();
//        DataSource dataSource1 = new DriverManagerDataSource(properties.getProperty("url"), properties);
////        jdbcTemplate = new JdbcTemplate();
//    }


}
